<template>
  <div>
    <el-drawer
      v-model="drawer"
      title="设置轮播图"
      :before-close="handleClose"
      size="50%"
      destroy-on-close
    >
      <el-form :model="form" label-width="80px">
        <el-form-item label="轮播图">
          <ChooseImage v-model="form.banners" :imgLimit="9"></ChooseImage>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submit">提交</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
  </div>
</template>
<script setup>
import { ref, reactive } from "vue";
import { readGoods, setGoodsBanners } from "@/api/goods";
import ChooseImage from "@/components/ChooseImage/ChooseImage.vue";
import { Toast } from "@/utils/utils.js";
let drawer = ref(false);
const form = reactive({
  banners: [],
});
let currentId = ref("");
// 打开
const open = (row) => {
  currentId.value = row.id;
  readGoods(row.id)
    .then((res) => {
      form.banners = res.data.goodsBanner.map((o) => o.url);
      drawer.value = true;
    })
    .catch((err) => {
      console.log(err);
    });
};
let emit = defineEmits(["submit"]);
const submit = () => {
  setGoodsBanners(currentId.value, {
    banners: form.banners,
  }).then((res) => {
    if (res.status === 200) {
      Toast("设置成功");
      drawer.value = false;
      emit("submit");
    }
  });
};
const handleClose = () => {
  drawer.value = false;
};
defineExpose({
  open,
});
</script>
